﻿using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.Redis
{
    /// <summary>
    /// Redis辅助类
    /// </summary>
    public static class RedisHelper
    {
        private static List<RedisInstance> redisInstances = new List<RedisInstance>();

        /// <summary>
        /// 获取Redis客户端实例
        /// </summary>
        /// <param name="name">指定的实例名称</param>
        /// <param name="conStr">连接字符串</param>
        /// <param name="db">数据库Id</param>
        /// <returns>指定名称的实例,如果实例不存在,则新增该实例。</returns>
        public static RedisInstance GetInstance(string name, string conStr, int db = -1)
        {
            // 查询实例
            var redisInstance = redisInstances.FirstOrDefault(p => p.Name == name);

            if (redisInstance != null)
            {
                return redisInstance;
            }

            // 创建新实例
            RedisInstance newInstance = new RedisInstance(name, conStr, db);
            redisInstances.Add(newInstance);

            return newInstance;

        }

    }
}
